package com.nt.oam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nt.common.core.domain.entity.SysRole;
import com.nt.common.core.domain.entity.SysUser;
import com.nt.common.enums.YesOrNoEnum;
import com.nt.common.utils.DateUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import com.nt.common.utils.SecurityUtils;
import com.nt.common.utils.StringUtils;
import com.nt.oam.domain.AntDatabaseAsset;
import com.nt.oam.domain.AntHospital;
import com.nt.oam.vo.project.*;
import com.nt.system.service.impl.SysRoleServiceImpl;
import com.nt.system.service.impl.SysUserServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.nt.oam.mapper.AntHospitalProjectMapper;
import com.nt.oam.domain.AntHospitalProject;
import com.nt.oam.service.IAntHospitalProjectService;

/**
 * 项目信息Service业务层处理
 *
 * @author JZFeng
 * @date 2025-02-05
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AntHospitalProjectServiceImpl extends ServiceImpl<AntHospitalProjectMapper, AntHospitalProject> implements IAntHospitalProjectService {
    private final AntHospitalProjectMapper antHospitalProjectMapper;
    private final SysRoleServiceImpl sysRoleService;

    private final AntHospitalServiceImpl antHospitalService;
    private final SysUserServiceImpl sysUserService;

    /**
     * 查询项目信息
     *
     * @param projectId 项目信息主键
     * @return 项目信息
     */
    @Override
    public AntHospitalProject selectAntHospitalProjectByProjectId(Long projectId) {
        return antHospitalProjectMapper.selectAntHospitalProjectByProjectId(projectId);
    }

    /**
     * 查询项目信息列表
     *
     * @param antHospitalProject 项目信息
     * @return 项目信息
     */
    @Override
    public List<AntHospitalProject> selectAntHospitalProjectList(AntHospitalProject antHospitalProject) {
        return antHospitalProjectMapper.selectAntHospitalProjectList(antHospitalProject);
    }

    /**
     * 新增项目信息
     *
     * @param antHospitalProject 项目信息
     * @return 结果
     */
    @Override
    public int insertAntHospitalProject(AntHospitalProject antHospitalProject) {
        antHospitalProject.setCreateTime(DateUtils.getNowDate());
        // 医院的id
        Long hospitalId = antHospitalProject.getHospitalId();
        antHospitalProject.setHospitalId(hospitalId);
        // 查询医院的名称
        AntHospital antHospital = antHospitalService.getById(hospitalId);
        if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
            antHospitalProject.setHospitalName(antHospital.getHospitalName());
        }
        // 创建人
        // 创建人
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (Objects.nonNull(sysUser) && StringUtils.isNotEmpty(sysUser.getUserName())) {
            antHospitalProject.setCreatePerson(sysUser.getUserName());
        }
        antHospitalProject.setIsDeleted(YesOrNoEnum.NO.getCode());
        antHospitalProject.setIsModify(YesOrNoEnum.NO.getCode());
        return antHospitalProjectMapper.insertAntHospitalProject(antHospitalProject);
    }

    /**
     * 修改项目信息
     *
     * @param antHospitalProject 项目信息
     * @return 结果
     */
    @Override
    public int updateAntHospitalProject(AntHospitalProject antHospitalProject) {
        antHospitalProject.setUpdateTime(DateUtils.getNowDate());
        // 医院的id
        Long hospitalId = antHospitalProject.getHospitalId();
        antHospitalProject.setHospitalId(hospitalId);
        // 查询医院的名称
        AntHospital antHospital = antHospitalService.getById(hospitalId);
        if (StringUtils.isNotEmpty(antHospital.getHospitalName())) {
            antHospitalProject.setHospitalName(antHospital.getHospitalName());
        }
        // 更新人
        // 创建人
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (Objects.nonNull(sysUser) && StringUtils.isNotEmpty(sysUser.getUserName())) {
            antHospitalProject.setUpdatePerson(sysUser.getUserName());
        }
        return antHospitalProjectMapper.updateAntHospitalProject(antHospitalProject);
    }

    /**
     * 批量删除项目信息
     *
     * @param projectIds 需要删除的项目信息主键
     * @return 结果
     */
    @Override
    public int deleteAntHospitalProjectByProjectIds(Long[] projectIds) {
        List<Long> projectIdsList = Arrays.asList(projectIds);
        projectIdsList.forEach(a -> {
            AntHospitalProject antHospitalProject = antHospitalProjectMapper.selectAntHospitalProjectByProjectId(a);
            antHospitalProject.setIsDeleted(YesOrNoEnum.YES.getCode());
            antHospitalProjectMapper.updateAntHospitalProject(antHospitalProject);
        });
        //antHospitalProjectMapper.deleteAntHospitalProjectByProjectIds(projectIds);
        return YesOrNoEnum.YES.getCode();
    }

    /**
     * 删除项目信息信息
     *
     * @param projectId 项目信息主键
     * @return 结果
     */
    @Override
    public int deleteAntHospitalProjectByProjectId(Long projectId) {
        return antHospitalProjectMapper.deleteAntHospitalProjectByProjectId(projectId);
    }

    /**
     * 获取医院名称列表
     */
    @Override
    public List<HospitalNameListVo> getHospitalNameList(String hospitalName) {
        return antHospitalProjectMapper.selectHospitalNameList(hospitalName);
    }

    /**
     * 获取销售/交付人名称
     */
    @Override
    public List<PersonListVo> getPersonNameList(String personName) {
        return antHospitalProjectMapper.selectPersonNameList(personName);
    }

    /**
     * 获取医院负责人名称
     */
    @Override
    public List<ChargeListVo> getChargeNameList(String chargeName) {
        return antHospitalProjectMapper.selectChargeNameList(chargeName);
    }

    /**
     * 获取内部人员名称
     */
    @Override
    public List<ChargeListVo> getInnerNameList(String innerName) {
        return antHospitalProjectMapper.selectInnerNameList(innerName);
    }

    /**
     * 获取项目名称列表
     */
    @Override
    public List<ProjectNameListVo> getProjectNameList(String itemName) {
        return antHospitalProjectMapper.selectProjectNameList(itemName);

    }

    /**
     * 查询项目的详情信息
     *
     * @param projectId 项目信息主键
     * @return 项目信息
     */
    @Override
    public ProjectDetailsVo selectProjectDetail(Long projectId) {
        AntHospitalProject project = antHospitalProjectMapper.selectAntHospitalProjectByProjectId(projectId);
        ProjectDetailsVo projectDetailsVo = new ProjectDetailsVo();
        // 判断医院项目是否存在
        if (Objects.nonNull(project)) {
            projectDetailsVo.setProjectId(project.getProjectId());
            AntHospital antHospital = antHospitalService.selectAntHospitalByHospitalId(project.getHospitalId());
            //医院名称
            if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
                projectDetailsVo.setHospitalName(antHospital.getHospitalName());
            }
            //项目名称
            projectDetailsVo.setProjectName(project.getProjectName());
            //销售姓名
            if (StringUtils.isNotEmpty(project.getSellName())) {
                projectDetailsVo.setSellName(project.getSellName());
            }
            //销售联系方式
            if (StringUtils.isNotEmpty(project.getSellPhone())) {
                projectDetailsVo.setSellPhone(project.getSellPhone());
            }
            // 交付人姓名
            if (StringUtils.isNotEmpty(project.getDeliveryName())) {
                projectDetailsVo.setDeliveryName(project.getDeliveryName());
            }
            // 交付人联系方式
            if (StringUtils.isNotEmpty(project.getDeliveryPhone())) {
                projectDetailsVo.setDeliveryPhone(project.getDeliveryPhone());
            }
            // 医院负责人
            if (StringUtils.isNotEmpty(project.getHospitalChargeName())) {
                projectDetailsVo.setHospitalChargeName(project.getHospitalChargeName());
            }
            //医院负责人联系方式
            if (StringUtils.isNotEmpty(project.getHospitalChargePhone())) {
                projectDetailsVo.setHospitalChargePhone(project.getHospitalChargePhone());
            }
            // 项目开始时间
            if (Objects.nonNull(project.getProjectStartTime())) {
                projectDetailsVo.setProjectStartTime(project.getProjectStartTime());
            }
            //项目结束时间
            if (Objects.nonNull(project.getProjectEndTime())) {
                projectDetailsVo.setProjectEndTime(project.getProjectEndTime());
            }
            //创建时间
            if (Objects.nonNull(project.getCreateTime())) {
                projectDetailsVo.setCreateTime(project.getCreateTime());
            }
            //创建人
            if (StringUtils.isNotEmpty(project.getCreatePerson())) {
                projectDetailsVo.setCreatePerson(project.getCreatePerson());
            }
            //更新时间
            if (Objects.nonNull(project.getUpdateTime())) {
                projectDetailsVo.setUpdateTime(project.getUpdateTime());
            }
            //更新人
            if (StringUtils.isNotEmpty(project.getUpdatePerson())) {
                projectDetailsVo.setUpdatePerson(project.getUpdatePerson());
            }
            projectDetailsVo.setIsDeleted(project.getIsDeleted());
        }
        return projectDetailsVo;
    }

    /**
     * 查询是否在修改
     */
    public int selectProjectModify(Long projectId) {
        AntHospitalProject antHospitalProject = this.getById(projectId);
        int isModify = 0;
        if (Objects.nonNull(antHospitalProject) && Objects.nonNull(antHospitalProject.getIsModify())) {
            isModify = antHospitalProject.getIsModify();
        }
        return isModify;
    }

    /**
     * 重新设置修改状态
     */
    public int updateProjectModify(Long projectId, Integer isModify) {
        AntHospitalProject antHospitalProject = this.getById(projectId);
        if (Objects.nonNull(antHospitalProject)) {
            antHospitalProject.setIsModify(isModify);
        }
        return antHospitalProjectMapper.updateAntHospitalProject(antHospitalProject);
    }
}
